package mk.com.ldm.corepoint.administration.client.view;

import java.util.ArrayList;

import mk.com.ldm.corepoint.administration.client.lib.CustomPagingScrollTable.CustomPagingScrollTable;
import mk.com.ldm.corepoint.administration.client.localization.CorePointConstants;
import mk.com.ldm.corepoint.administration.client.presenter.MainPresenter;

import org.gwt.mosaic.ui.client.PopupMenu;
import org.gwt.mosaic.ui.client.ToolButton;
import org.gwt.mosaic.ui.client.ToolButton.ToolButtonStyle;
import org.gwt.mosaic.ui.client.layout.BoxLayout;
import org.gwt.mosaic.ui.client.layout.BoxLayout.Orientation;
import org.gwt.mosaic.ui.client.layout.BoxLayoutData;
import org.gwt.mosaic.ui.client.layout.BoxLayoutData.FillStyle;
import org.gwt.mosaic.ui.client.layout.LayoutPanel;
import org.gwt.mosaic.ui.client.table.FixedWidthFlexTable;
import org.gwt.mosaic.ui.client.table.FixedWidthGrid;
import org.gwt.mosaic.ui.client.table.ScrollTable;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class MainView extends Composite implements MainPresenter.Display {

	private final FlexTable container;
	private final FlexTable header;
	private final FlexTable sidebar;
	private final FlexTable main;
	private final Button logOut;
	private final ArrayList<Widget> navigation;
	private final Label loggedIn;
	private LayoutPanel list;
	private final PopupMenu selectMenu;
	private final PopupMenu selectLangMenu;
    private FixedWidthFlexTable headerTable = new FixedWidthFlexTable();
    private FixedWidthGrid dataTable = new FixedWidthGrid();
    private CustomPagingScrollTable table;
	
	public MainView(){
		CorePointConstants constants = GWT.create(CorePointConstants.class);
		
		container = new FlexTable();
		container.getFlexCellFormatter().setColSpan(0, 0, 2);
		container.setWidth("100%");
		container.setCellSpacing(0);
		container.setCellPadding(0);
		container.getFlexCellFormatter().setWidth(0, 0, "100%");
		container.getFlexCellFormatter().setStyleName(0, 0, "mainHeader");
		container.getFlexCellFormatter().setStyleName(1, 0, "sidebar");
		container.getFlexCellFormatter().setWidth(1, 0, "120px");
		container.getFlexCellFormatter().setHeight(1, 0, Integer.toString(Window.getClientHeight())+"px");
		container.getFlexCellFormatter().setWidth(1, 1, "100%");
		container.getFlexCellFormatter().setHeight(1, 1, "100%");
		container.setStyleName("content");
		initWidget(container);
		
		header = new FlexTable();
		Label title = new Label(constants.title());
		title.setStyleName("title");
		Label placeHolder = new Label();
		loggedIn = new Label("Logged in as:");
		logOut = new Button("Log-out");
		placeHolder.setWidth("90%");
		header.setWidth("100%");
		header.setHeight("36px");
		header.setWidget(0, 0, title);
		header.getCellFormatter().setWidth(0, 0, "240px");
		header.setWidget(0, 1, placeHolder);
		header.getCellFormatter().setWidth(0, 1, "70%");
		header.setWidget(0, 2, loggedIn);
		header.setWidget(0, 3, logOut);
		
		
		navigation = new ArrayList<Widget>();
		navigation.add(new Label("Projects"));
		navigation.add(new Button("New project"));
		
		ToolButton select = new ToolButton("Select project...");
		selectMenu = new PopupMenu();
		select.setMenu(selectMenu);
		select.setStyle(ToolButtonStyle.SPLIT);
		
		
		placeHolder.setHeight("100%");
		placeHolder.setStyleName("placeholder");
		
		navigation.add(select);
		navigation.add(new Label("Pages"));
		navigation.add(new Button("New page"));
		navigation.add(new Button("List pages"));
		navigation.add(new Label("Users"));
		navigation.add(new Button("New user"));
		navigation.add(new Button("List users"));

		navigation.add(new Label("Languages"));
		navigation.add(new Button("New language"));
		
		ToolButton selectLang = new ToolButton("Select language...");
		selectLangMenu = new PopupMenu();
		selectLang.setMenu(selectLangMenu);
		selectLang.setStyle(ToolButtonStyle.SPLIT);
		
		navigation.add(selectLang);
		
		navigation.add(new Label("Application settings"));
		navigation.add(new Button("Edit settings"));
		
		navigation.add(placeHolder);
		
		sidebar = new FlexTable();
		int i = 0;
		for(Widget w:navigation){
			sidebar.setWidget(i, 0, w);
			sidebar.getFlexCellFormatter().setHeight(i, 0, "20px;");
			i++;
		}
		
		main = new FlexTable();
		main.setWidth("98%");
		main.setHeight(Integer.toString(Window.getClientHeight()-82)+"px");
		main.setStyleName("main");
		
		list =  new LayoutPanel(new BoxLayout(
		        Orientation.VERTICAL));
	    list.setPadding(0);

	    FixedWidthFlexTable scrollTableHeader = new FixedWidthFlexTable();
	    scrollTableHeader.setWidget(0, 0, new CheckBox());
	    scrollTableHeader.setCellPadding(0);
	    scrollTableHeader.setCellSpacing(0);
	    
//	    scrollTableHeader.setText(0, 1, "ID");
	    scrollTableHeader.setText(0, 1, "Name");
	    scrollTableHeader.setText(0, 2, "Controls");
	    
	    	    
//	    FixedWidthGrid dataTable = new FixedWidthGrid(30, 4);
//	    	    
//	    dataTable.setCellPadding(0);
//	    dataTable.setCellSpacing(0);
//	   
//	    int tableWidth = Window.getClientWidth()-200;
//	    int selectCol = (tableWidth * 5)/100;
//	    int nameCol = (tableWidth * 65)/100;
//	    int controlCol = (tableWidth * 30)/100;
//	    
//	    ScrollTable pagingScrollTable = new ScrollTable(dataTable, scrollTableHeader);
//	    pagingScrollTable.setWidth(Integer.toString(Window.getClientWidth()-230)+"px");
//	    pagingScrollTable.setHeight(Integer.toString(Window.getClientHeight()-140)+"px");
//	    pagingScrollTable.setColumnSortable(0, false);
//	    pagingScrollTable.setColumnSortable(3, false);
//	    pagingScrollTable.setColumnWidth(0, selectCol);
//	    pagingScrollTable.setColumnWidth(1, nameCol);
//	    pagingScrollTable.setColumnWidth(2, controlCol);
	    
	    table = new CustomPagingScrollTable(dataTable, headerTable);
	    table.setWidth(Integer.toString(Window.getClientWidth()-230)+"px");
	    table.setHeight(Integer.toString(Window.getClientHeight()-140)+"px");
	    list.add(table, new BoxLayoutData(FillStyle.BOTH));
	    
	    list.setStyleName("hide");
	    
		main.setWidget(0, 0, list);
		
		container.setWidget(0, 0, header);
		container.setWidget(1, 0, sidebar);
		container.setWidget(1, 1, main);
		
		
		
	}
	
	@Override
	public Widget asWidget() {
	    return this;
	}

	public Button getLogout(){
		return logOut;
	}
	
	public Label getLoggedIn(){
		return loggedIn;
	}
	
	public ArrayList<Widget> getSidebar(){
		return navigation;
	}
	
	public LayoutPanel getList(){
		return list;
	}
	
	public PopupMenu getListMenu(){
		return selectMenu;
	}
	
	public PopupMenu getListLangMenu(){
		return selectLangMenu;
	}

	@Override
	public FixedWidthGrid getDataTable() {
		return dataTable;
	}

	@Override
	public FixedWidthFlexTable getHeaderTable() {
		return headerTable;
	}

	@Override
	public CustomPagingScrollTable getTable() {
		return table;
	}
}
